<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org"
      data-layout-decorator="common/layout/layout">
<head>
    <title>用户列表</title>
</head>

<body>

    <!-- data-layout-fragment="content" 标签内的内容会替换 layout 页面中 data-layout-fragment="content" 面的内容 -->
    <div data-layout-fragment="content">
        <div class="container-fluid">

            <h4>用户列表</h4>

            <table class="table table-bordered">
                <tr>
                    <td>ID</td>
                    <td>登录名</td>
                    <td>密码</td>
                    <td>姓名</td>
                    <td>是否禁止登录</td>
                    <td>操作</td>
                </tr>
                <tr th:each="user,userStat : ${userList}">
                    <td th:text="${user.id }" ></td>
                    <td th:text="${user.loginName }" ></td>
                    <td th:text="${user.password }" ></td>
                    <td th:text="${user.name }" ></td>
                    <td th:text="${user.loginFlag eq 0 ? '否' : '是' }" ></td>
                    <td>
                        <button type="button" class="btn btn-primary btn-xs">修改</button>
                        <button type="button" class="btn btn-danger btn-xs">删除</button>

                        <th:block th:if="${user.loginFlag eq 0 }">
                            <button type="button" class="btn btn-link btn-xs">禁用</button>
                        </th:block>
                        <th:block th:if="${user.loginFlag eq 1 }">
                            <button type="button" class="btn btn-link btn-xs">启用</button>
                        </th:block>

                    </td>
                </tr>
            </table>

            <div>
                <form th:action="@{/a/sys/user/gotoUserForm}" th:method="post">
                    <button id="addUserBtn" type="submit" class="btn btn-default">增加用户</button>
                    <button id="ajaxFindAllUser" type="button" class="btn btn-default">ajax 查询用户</button>
                    <button id="ajaxAddUser" type="button" class="btn btn-default">ajax 增加用户</button>
                    <button id="sendMessage" type="button" class="btn btn-default">发送消息到 Redis 的消息队列</button>
                </form>
            </div>
        </div>
    </div>

    <!-- data-layout-fragment="js" 标签内的内容会替换 layout 页面中 data-layout-fragment="js" 的内容 -->
    <script type="text/javascript" th:inline="javascript" data-layout-fragment="js">
        $(function () {

            $("#ajaxFindAllUser").click(function(){
                $.ajax({
                    url:"/a/sys/user/ajaxFindAllUser",
                    type:"post",
                    dataType:"json",
                    success:function(data){
                        for(var i=0; i<data.length; i++){
                            var user = data[i];
                            alert(user.loginName);
                        }
                    }
                });
            });

            $("#ajaxAddUser").click(function(){
                $.ajax({
                    url:"/a/sys/user/save",
                    type:"post",
                    dataType:"json",
                    success:function(data){
                        AjaxError.unauthorized(data.code, data.message);
                    },
                    error:function(data){
                        AjaxError.unauthorized(data.code, data.message);
                    }
                });
            });

            $("#sendMessage").click(function () {
                $.post(
                    "/a/sys/user/sendMessage",
                    function (data) {
                        alert(data);
                    },
                    "json"
                );
            });

        });

    </script>
</body>
</html>